package _1_PIO.excel.样式;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 单元格使用换行符，并自适应列宽
 */
public class _7_NewLinesInCells {

    @Test
    public void _2003(){

    }

    @Test
    public void _2007() throws IOException {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("newLinesInCells");
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellValue("Use \n with word wrap on to create a new line");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setWrapText(true);
        cell.setCellStyle(cellStyle);

        //变为2倍行高
        row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));

        //列宽自适应（参数为列索引，从0开始）
        sheet.autoSizeColumn(1);

        try(OutputStream outputStream = new FileOutputStream("D://workbook.xlsx")){
            workbook.write(outputStream);
        }
        finally {
            workbook.close();
        }
    }

}
